package com.changgou.web.gateway.filter;

import com.changgou.web.gateway.service.AuthService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @Author: HuangFu
 * @Date: 2020/2/11 10:53
 * @Description: 授权认证过滤器
 */
@Component
public class AuthFilter implements GlobalFilter, Ordered {

    private final static String AUTH_LOGIN_URL = "/api/oauth/toLogin";

    @Autowired
    private AuthService authService;
    @Autowired
    private UrlFilter urlFilter;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //获取请求对象
        ServerHttpRequest request = exchange.getRequest();

        //获取响应对象
        ServerHttpResponse response = exchange.getResponse();

        //判断当前请求是否为登录请求，是的话，则放行
        String path = request.getURI().getPath();
        //获取请求前缀部分
        String headPath = String.valueOf(request.getURI()).replace(path,"");

        if(AUTH_LOGIN_URL.equals(path) || !urlFilter.hasAuthorize(path)){
            return chain.filter(exchange);
        }

        //判断cookie中是否存在信息, 没有的话，拒绝访问
        String jtl = authService.getCookieValue(request, "uid");
        if(StringUtils.isEmpty(jtl)){
            //设置响应结果，结束访问
            /*response.setStatusCode(HttpStatus.UNAUTHORIZED);
            return response.setComplete();*/
            //跳转登录页面
            return this.toLoginPage(headPath+AUTH_LOGIN_URL+"?FROM="+request.getURI().getPath(),exchange);
        }

        //判断redis中令牌是否存在，没有的话，拒绝访问
        String jwtToken = authService.getJwtToken(jtl);
        if(StringUtils.isEmpty(jwtToken)){
            //设置响应结果，结束访问
            /*response.setStatusCode(HttpStatus.UNAUTHORIZED);
            return response.setComplete();*/
            //跳转登录页面
            return this.toLoginPage(headPath+AUTH_LOGIN_URL+"?FROM="+request.getURI().getPath(),exchange);
        }

        //对当前的请求对象进行增强,让它会携带令牌的信息
        //request.getHeaders().add("Authorization","Bearer "+jwtToken);
        request.mutate().header("Authorization","Bearer "+jwtToken);

        //放行
        return chain.filter(exchange);
    }

    private Mono<Void> toLoginPage(String authLoginUrl, ServerWebExchange exchange) {
        ServerHttpResponse response = exchange.getResponse();
        //设置跳转状态
        response.setStatusCode(HttpStatus.SEE_OTHER);
        //设置响应跳转
        response.getHeaders().set("Location",authLoginUrl);
        return response.setComplete();
    }



    @Override
    public int getOrder() {
        return 0;
    }


}
